package com.srs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.NiData;
import com.srs.util.HibernateUtil;

public class NiDataDao {

	final static Logger logger = LoggerFactory.getLogger(NiDataDao.class);

	private List<NiData> niDataList;
	private NiData niData;

	public void delete(Integer id) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			niData = (NiData) session.get(NiData.class, id);
			session.delete(niData);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<NiData> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			niDataList = session.createQuery("from NiData").list();
			return niDataList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<NiData> list(String msisdn) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from NiData nd where nd.msisdn=:msisdn order by nd.insertedTime desc");
			q.setString("msisdn", msisdn);
			niDataList = q.list();
			return niDataList;
		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}
	}
	
	
	public NiData get(Integer id) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User u where u.uid=:id");
			q.setInteger("userid", id);
			return (NiData) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public NiData getNewest(String msisdn){
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from NiData qs where qs.msisdn=:msisdn order by qs.insertedTime desc");
			q.setString("msisdn", msisdn);
			niDataList = q.list();
			
			if (niDataList==null) return null;
			return niDataList.get(0);

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}		
	}	
	
	public void insert(NiData usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(NiData usr) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(usr);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
